﻿@using AplicacionBase.Models

@{
    ViewBag.Title = "Llenar encuesta";
    var wizard = ViewBag.WizardStep;
    if (wizard == 1)
    {
        Layout = "~/Views/Shared/_LayoutW.cshtml";
        wizard = 1;
    }
}

<script type="text/javascript" src="http://jetlogs.org/jquery/jquery-1.2.6.pack.js"></script>

<div>
	
		<h3>Responda las preguntas</h3>
        

		@using (Html.BeginForm())
  {
	
			<fieldset>
		
				@{
      int i = 1;
				}
		
				@foreach (SurveysTopic surveysTopic in ViewBag.questions)
    {
					@:<div class="row space-bot">
					@:<div class="ttema">
					@: <h3>Tema: @surveysTopic.Topic.Description </h3>
					@:</div>
        foreach (Question question in surveysTopic.Topic.Questions)
        {
            string cl = "";
            List<string> code = new List<string>();
            List<string> codetext = new List<string>();
            if (question.IsObligatory == 1)
            {
                cl = "required";
            }
            if (question.Type == "Multiple" && question.IsObligatory ==1)
            {
                foreach (var answerChoice in question.AnswerChoices)
                {
                    if (answerChoice.Type == "Con texto")
                    {
                        codetext.Add("tt" + answerChoice.Id);
                    }
                    else
                    {
                        code.Add("mu" + answerChoice.Id);
                    }

                }
            

                <text><script type="text/javascript">
                          $(document).ready(function() {
                              $("form").submit(function() {
                                  if (@for (int j = 0; j < code.Count; j++)
                                      {
                                          if (j == 0)
                                          {
                                              @:!isCheckedById("@code[j]")
                                          }
                                          else
                                          {
                                              @: && !isCheckedById("@code[j]")				            
                                          }
                                      }
                                  @for(int k =0;k<codetext.Count; k++)
                                      {
                                          if (k == 0)
                                          {
                                              @:  && ( $("#@codetext[k]").attr('value') == ''
                                          }
                                          else
                                          {
                                            
                                                    @: && ( $("#@codetext[k]").attr('value') == ''
                                                
                                            }

                                            if(k==codetext.Count-1)
                                            {
                                                    @: )
                                            }
                                          }

                                  ) {
                                        //alert("Please select at least one selector");
                                        $("#@question.Id").text("Esta pregunta es obligatoria, debe marcar al menos una opción");
                                  
                                        return false;
                                    } else {
                                        $("#@question.Id").text("");
                                        return true; //submit the form
                                    }
                                    });

                                        function isCheckedById(id) {
                                        var checked = $("input[@@id=" + id + "]:checked").length;
                                        if (checked == 0) {
                                        return false;
                                    } else {
                                        return true;
                                    }
                                    }
                                    });
                      </script>
                
                </text>

        }

            if (question.Type == "Unica")
            {
                List<string> c1 = new List<string>();
                List<string> c2 = new List<string>();
                string que = "un"+question.Id.ToString();
                foreach (var choice in question.AnswerChoices )
                {
                    if (choice.Type != "Normal")
                    {
                        c2.Add("tt" + choice.Id.ToString());
                    }
                }

                foreach (var vartexto in c2)
                {
<text>
    <script type="text/javascript">
        $(document).ready(function () {
            $("input[@@id=@que]").change(
                function () { $("#@vartexto").val(''); }
            );

            $("input[@@id=@vartexto]").change(
                function () {
                    if ($("#@vartexto").attr('value') == '') {

                        $("input[@@id=@que]").attr('checked', true);
                    }
                    else {

                        $("input[@@id=@que]").attr('checked', false);
                    }
                }
            );
        });
    </script>

</text>
                }
            }
            
            if ((question.Type == "Corta" || question.Type == "Larga") && question.IsObligatory == 1)
            {
                if (question.AnswerChoices.Count>0)
                {
                    string ida = "tt" + question.AnswerChoices.First().Id.ToString();
                    
                    <text>
                        <script type="text/javascript">
                                  $(document).ready(function () {
                                      $('form').submit(function () {
                                          if (($("#@ida").attr('value') == '')) {
                                              $("#@question.Id").text("Esta pregunta es obligatoria, debe marcar al menos una opción");
                                              return false;
                                          }
                                          else {
                                              $("#@question.Id").text("");
                                          return true;
                                      }
                                      });
                                  });
                              </script>
                    </text>
                }
                
            }

						@*@:<div class="row space-bot">*@
						       @:<div class="c12">
					                @:<p>
					                @:Pregunta @i : @question.Sentence
						                            @*@Html.Label(question.Sentence)  *@
					                @:</p>
                                @:</div>             
						@*@:</div>*@
            foreach (AnswerChoice answer in question.AnswerChoices)
            {
                            string label = answer.Sentence.ToString();
						    @:<div class="row space-bot">
							if (answer.Type == "Normal")
							{
							    
                                if (question.Type == "Unica")
                                {
								    @:<div class="c1-5">
                                    													  
									@*@:<span class="etiqueta">*@
									@label 
									@*@:</span>*@
                                        @:</div>
                                    @:<div class="c1-5">
									@*@:<span class="form-el">  *@    
									@Html.RadioButton("un" + question.Id.ToString(), answer.Id.ToString(), true)
									@*@:</span> *@

									@:</div>

                                }
                                if (question.Type == "Multiple")
                                {
                                    
                                    @:<div class="c1-5">
									@*@:<div class="fila">                          
									@:<span class="etiqueta">*@   
                                    @label            
									@*@Html.Label(answer.Sentence)*@
									@*@:</span>
                                    
									@:<span class="form-el"> *@ 
								    @:</div> 
                                    @:<div class="c1-5">
									@Html.CheckBox("mu" + answer.Id.ToString(), "false")
									@*@:</span>*@
									@:</div> 
								}
                                if (question.Type == "Corta")
                                {
									@*@:<div class="c1-5">*@
							        @*@:<span class="etiqueta"> 
							        @Html.Label(answer.Sentence);*@
						            @* @:</span>*@
									@*@:<span class="form-el"> *@  
                                   @* @:</div> *@
                                    @:<div class="c1-5">
									@Html.TextBox("tt" + answer.Id.ToString(), "", new { style = "width:400px" })
									@*@:</span> *@
									@:</div>  
								}

                                if (question.Type == "Larga")
                                {
                                    @*@:<div class="c1-5">*@
						            @* @:<span class="etiqueta"> 
							        @Html.Label(answer.Sentence)
							        @:</span>*@
							        @*@:</div>
							        @:<div class="fila">*@
									@*@:<span class="form-el"> *@
                                    @*@:</div>*@
                                    @:<div class="c1-5">
									@Html.TextArea("tt" + answer.Id.ToString(), new { style = "width:700px;height:100px" })                              
									@*@:</span> *@
									@:</div>  
								}
                            }
                            else
                            {
                                @:<div class="c1-5">
								@*@:<div class="fila"> 
								@:<span class="etiqueta">*@
                                @label
								@*@Html.Label(answer.Sentence)*@
								@*@:</span> 
								@:<span class="form-el">*@
                                @:</div> 
                                @:<div class="c1-5">
								@Html.TextBox("tt" + answer.Id.ToString(), "", new { style = "width:400px" })
								@*@:</span> *@
								@:</div>
							}  
                            @:</div>              
						}
                        
            <div class="alert error"><label id="@question.Id"></label></div>
            <br />


            i++;
        } 
                            
					@:</div>           
				}		
				<div class="row space-bot"> 
					<span class="etiqueta">
					</span>
					<span class="form-el">
					    <input type="submit" value="Guardar" class="ebutton2 blue" />
                       
					</span>
				</div>
	
		
		
			</fieldset>
   
  }
</div>

